<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;
use Home\Model\PlateModel;
use Home\Model\PlatesqlModel;

class PlateController extends BaseController {

	/**
	 * 实例化模型对象1
	 */
	protected function newmodle(){
		return new PlateModel();
	}

	public function index($rootid, $plateid = null){
		//根据rootid获取此条信息
		$type_info  = $this->id_get_type_info($rootid);
		$plate_result = $this->get_custom_plate($type_info['plate_id']);
		$this->assign('plate_resource_info', $plate_result);

		$plate_onley_info = $this->get_palte_info($plateid);
		$this->assign('plate_onley_info', $plate_onley_info);
		$this->plateid_contentlist($plateid);
		$this->display();
	}

	public function index1($rootid, $plateid = null){

		$plate = D(C('TABLE_PLATEMANAGES'));
		$shelf_info = $plate->find($plateid);
		$res = $plate->where(array('mid' => $plateid))->order('plate_sort asc')->select();
		if($res){
			foreach($res as &$v){
				$v['sub_article'] = $this->get_paltelist($v['id']);
			}
		}
		$this->assign('plate_sub_list', $res);
		$this->assign('shelf_info', $shelf_info);
		$this->display();
	}

	protected function get_paltelist($plate_id){
		$pcontent = D(C('TABLE_PLATECONTENT'));
		$res = $pcontent->where(array('plate_id' => $plate_id))->select();
		return $res;
	}

	/**
	 * 一般详情页
	 */
	public function article($arid){
		//echo "aa";die;
		$plate = $this->newmodle();
		$article_id_info = $plate::get_article_id_info($arid);
		if($article_id_info['plate_id'] == 112){
			$gzh_link = '<a style=\'color: #f00;\' href=\'https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzIxMjA0MjkwNQ==&scene=124#wechat_redirect\'>点击关注靖边农产品掌上服务宝</a>';
		}
		$this->assign('co_gzh', $gzh_link);
		$jumplink = $article_id_info['jumplink'];
		if(!empty($jumplink)){
			//header("Location:$jumplink");
			header("Location:http://www.hrtv.cn/daiyan/42296.html");
		}else{
			$this->assign('article', $article_id_info);
			$this->assign('typeinfo', $this->get_mainfo($article_id_info));
			$this->display();
		}		
	}

	/**
	 * 根据全局ROOTID值获取码信息
	 */
	protected function get_mainfo($article_id_info){
		$g_rootid = session('glob_rootid');
		if(!empty($g_rootid)){
			$type = D(C('TABLE_TYPE'));
			$res = $type->find(session('glob_rootid'));
			$result = $res['typename'];
		}else{
			$result = $article_id_info['title'];
		}
		return $result;
	}

	/**
	 * 历史上的今天详细内容
	 */
	public function ahistory($eid){
		$plate = $this->newmodle();
		$history_id_info = $plate::get_eid_content($eid);
		$this->assign('history_article', $history_id_info);
		$this->display();
	}

	/**
	 * 实例化模型对象2
	 */
	protected function platesqlmodle(){
		return new PlatesqlModel();
	}

	/**
	 * 根据版块ID获取版块内容并保存数据库
	 */
	public function getresure_querysql(){
		set_time_limit();
		$plate = $this->newmodle();
		$platesql = $this->platesqlmodle();

		$history_list_info = $plate::history_list_info();
		foreach($history_list_info as $key => $val){
			if($key < 4){
				$history_id_array_info[] = $plate::history_id_info($history_list_info[$key]['e_id']);
			}
		}
		foreach($history_id_array_info as $key => &$v){
			$pic 				= !getimagesize($v['picUrl'][0]['url']) ? null : $v['picUrl'][0]['url'];
			$v['plate_id'] 		= 52;
			$v['pic'] 			= $pic;
			$v['content'] 		= ltrim($v['content'], chr(0xc2).chr(0xa0));
			$v['r_date'] 		= null;
			$v['author_name'] 	= null;
			$v['r_url'] 		= null;
		}
		//dump($history_id_array_info);
		if($platesql::resure_querysql($history_id_array_info)){
			$get_weixin_first_info = $plate::weixin_list_info();
			foreach($get_weixin_first_info as $key => &$v){
				$pic 				= !getimagesize($v['firstImg']) ? null : $v['firstImg'];
				$v['plate_id'] 		= 53;
				$v['pic'] 			= $pic;
				$v['content'] 		= null;
				$v['r_date'] 		= null;
				$v['author_name'] 	= $v['source'];
				$v['r_url'] 		= $v['url'];
				$v['e_id'] 			= null;
			}
			//dump($get_weixin_first_info);
			if($platesql::resure_querysql($get_weixin_first_info)){
				$get_news_first_info = $plate::news_list_info();
				foreach($get_news_first_info as $key => &$v){
					$pic 				= !getimagesize($v['thumbnail_pic_s']) ? null : $v['thumbnail_pic_s'];
					$v['plate_id'] 		= 56;
					$v['pic'] 			= $pic;
					$v['content'] 		= null;
					$v['r_date'] 		= $v['date'];
					$v['r_url'] 		= $v['url'];
					$v['e_id'] 			= null;
				}
				//dump($get_news_first_info);
				if($platesql::resure_querysql($get_news_first_info)){
					$get_xiaohua_first_info = $plate::xiaohua_list_info();
					foreach($get_xiaohua_first_info as $key => &$v){
						$v['plate_id'] 		= 57;
						$content = $v['content'];
						$title = msubstr1($content, 0, 20);
						$v['title'] 		= $title;
						$v['pic'] 			= null;
						$v['content'] 		= $content;
						$v['r_date'] 		= $v['updatetime'];
						$v['author_name'] 	= null;
						$v['r_url'] 		= null;
						$v['e_id'] 			= null;
					}
					//dump($get_xiaohua_first_info);
					$platesql::resure_querysql($get_xiaohua_first_info);
				}
			}
		}

	}


}